package proteinprediction.arffgenerator;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class OldArffGenerator extends AbstractArffGenerator{
	
	public OldArffGenerator(String listPath, String structPath, String resultPath, String considerZeros) {
		try {
			this.proteins = createProteinListFromPath(listPath);
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.structPath = structPath;
		this.resultPath = resultPath;
		if (considerZeros.equals("true")) {
			this.considerZeros = true;
		} else {
			this.considerZeros = false;
		}
	}

	public void generateArff() {
		try {
			for (String singleProtein : proteins) {
				if (isIMPFile(singleProtein)) {
					String path = structPath + createStructFromName(singleProtein);
					BufferedReader reader = new BufferedReader(new FileReader(path));
					reader.readLine();
//					String line = reader.readLine();
//					try {
//						createArffFileForIMP(getRelevantCharsFromLine(line), singleProtein);
					reader.readLine();
					reader.readLine();
					String line = reader.readLine();
					String arffAbsolutePath = resultPath + singleProtein + System.getProperty("file.separator") + "prot.arff";
					super.createArffFileForIMP(line.toCharArray(), arffAbsolutePath);
//					} catch (IllegalFileException e) {
//						System.out.println("Structure file " + singleProtein + " is not divisible by 3");
//						e.printStackTrace();
//					}
					reader.close();
				} else {
					String arffAbsolutePath = resultPath + singleProtein + System.getProperty("file.separator") + "prot.arff";
					String solQueryInPath = resultPath + singleProtein + System.getProperty("file.separator") + "query.in";
					super.createArffFileForSOL(singleProtein, arffAbsolutePath, solQueryInPath);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}

	private boolean isIMPFile(String name) {
		String[] temp = name.split("_|\\.");
		return (temp[0].equalsIgnoreCase("imp"));
	}
	
	private String createStructFromName(String singleProtein) {
		String result = "";
		String[] temp = singleProtein.split("_|\\.");
		result = "IMP_seq_ali_struct_" + temp[2] + ".fasta";
		return result;
	}

	private List<String> createProteinListFromPath(String path) throws IOException {
		List<String> result = new ArrayList<String>();
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String thisLine;
		while ((thisLine = reader.readLine()) != null) {
			result.add(thisLine);
		}
		return result;
	}
}
